/*
 * miBus.js
 */

/*----------------------------------------------
			MENU OPENERS
 ----------------------------------------------*/

$("#user, #mibusandando, #planeomiviaje, #misreservas, #contact_us, #elements, #rutasFilter").on("click tap", function(event){
	event.preventDefault();
	if (!$(this).hasClass("active")) {
		if ($(this).attr("id") == "user") {
			$("#mibusandando.active, #planeomiviaje.active, #misreservas.active, #contact_us.active, #elements.active, #rutasFilter.active").each(function(){
				$(this).removeClass("active");
				$("#" + $(this).attr("id") + "_menu").fadeOut({duration: 200});
			});
		}
		else if ($(this).attr("id") == "mibusandando") {
			$("#misreservas.active, #user.active, #planeomiviaje.active, #contact_us.active, #elements.active, #rutasFilter.active").each(function(){
				$(this).removeClass("active");
				$("#" + $(this).attr("id") + "_menu").fadeOut({duration: 200});
			});
		}
		else if ($(this).attr("id") == "planeomiviaje") {
			$("#misreservas.active, #user.active, #mibusandando.active, #contact_us.active, #elements.active, #rutasFilter.active").each(function(){
				$(this).removeClass("active");
				$("#" + $(this).attr("id") + "_menu").fadeOut({duration: 200});
			});
		}
		else if ($(this).attr("id") == "misreservas") {
			$("#mibusandando.active, #planeomiviaje.active, #contact_us.active, #elements.active, #rutasFilter.active").each(function(){
				$(this).removeClass("active");
				$("#" + $(this).attr("id") + "_menu").fadeOut({duration: 200});
			});
		}
		else if ($(this).attr("id") == "contact_us") {
			$("#mibusandando.active, #planeomiviaje.active, #user.active, #misreservas.active, #elements.active, #rutasFilter.active").each(function(){
				$(this).removeClass("active");
				$("#" + $(this).attr("id") + "_menu").fadeOut({duration: 200});
			});
		}
		else if ($(this).attr("id") == "rutasFilter") {
			$("#mibusandando.active, #planeomiviaje.active, #contact_us.active, #user.active, #misreservas.active, #elements.active").each(function(){
				$(this).removeClass("active");
				$("#" + $(this).attr("id") + "_menu").fadeOut({duration: 200});
			});
		}
		$("#mapapopups_menu").fadeOut({duration: 200});
	}
	else {
		if ($(this).attr("id") == "user") {
			$("#misreservas.active").each(function(){
				$(this).removeClass("active");
				$("#" + $(this).attr("id") + "_menu").fadeOut({duration: 200});
			});
		}
	}
	$(this).toggleClass("active");
	$("#" + $(this).attr("id") + "_menu").fadeToggle({duration: 200});
});

var closeAllMenus = function () {
	$("#user.active, #mibusandando.active, #planeomiviaje.active, #misreservas.active, #contact_us.active, #elements.active, #rutasFilter.active").each(function(){
		$(this).removeClass("active");
		$("#" + $(this).attr("id") + "_menu").fadeOut({duration: 200});
	});
}

/*----------------------------------------------
		DISPLAYING NOTIFICATIONS FUNCTION
 ----------------------------------------------*/

function displayNotification(msg, type) {
	var $notifications = $("#notifications");
	$("#msg", $notifications).html(msg);
	$notifications.show();
}

 /*----------------------------------------------
			SYSTEM INFO LINK
 ----------------------------------------------*/

var a_clicked = false;
$("#system_info").on("click tap", function() {
	if (!a_clicked) {
		a_clicked = true;
		$("a", this)[0].click();
	}
	else {
		a_clicked = false;
	}
});

$("#system_info a").on("click tap", function() {
	a_clicked = true;
});

/*----------------------------------------------
			CONTACTENOS MENU
 ----------------------------------------------*/

var beforeSendContacto = function() {
	var $submitbtn = $("#contact_us_menu form input[type=submit]");
	$submitbtn.attr({
		value: 'Enviando',
		disabled: 'disabled'
	});
	eraseErrorMessages("Contactenos");
};

var successSendContacto = function(data, textStatus) {
	if (data.errors != undefined) {
		$.each(data.errors, function(property, value){
			createErrorMessage("Contactenos", property, value[0]);
		});
	}
	else {
		eraseErrorMessages("Contactenos");
		eraseInputContents("Contactenos");
		displayNotification(data.message);
		$("#contact_us").click();
	}
};

var errorSendContacto = function(jqXHR, textStatus, errorThrown) {
	displayNotification("Ocurrio un error al enviar el correo (" + errorThrown + ": " + textStatus + ")");
	$("#contact_us").click();
};

var completeSendContacto = function() {
	var $submitbtn = $("#contact_us_menu form input[type=submit]");
	$submitbtn.attr({
		value: 'Enviar'
	});
	$submitbtn.removeAttr('disabled');
};

/****************************************************
			OTHER FUNCTIONS
****************************************************/

var createErrorMessage = function(model, field, errorMessage) {
	$("input[name='data[" + model + "][" + field + "]'], textarea[name='data[" + model + "][" + field + "]']").after('<div class="error-message">' + errorMessage + '</div>');
	$("input[name='data[" + model + "][" + field + "]'], textarea[name='data[" + model + "][" + field + "]']").parent().addClass("error");
};

var eraseErrorMessages = function(model) {
	$("input[name^='data[" + model + "]'], textarea[name^='data[" + model + "]']").each(function(){
		$(this).siblings("div.error-message").remove();
		$(this).parent().removeClass("error");
	});
};

var eraseInputContents = function(model) {
	$("input[name^='data[" + model + "]'], textarea[name^='data[" + model + "]']").each(function(){
		$(this).val("");
	});
};